import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { AuditService } from 'src/app/service/audit.service';
import { FollowInfo, JsonResult } from 'src/app/global/models';
import { NzNotificationService } from 'ng-zorro-antd';
import { APPID } from 'src/app/global/globalconst';


@Component({
  selector: 'app-audit',
  templateUrl: './audit.component.html',
  styleUrls: ['./audit.component.css']
})
export class AuditComponent implements OnInit {

  mill:string;
  clientId:number;
  clientSid:string;
  clientName:string;
  nickName:string;
  openId:string;
  followTime:Date;
  realName:string;
  code:string;

  // 检测当前页面没有 code 时要跳转到有 code 的页面
  url:string = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=https://www.xdfznh.club/wxpadfe/auditUser&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';

  constructor(
    private http:HttpClient,
    private route:ActivatedRoute,
    private service:AuditService,
    private notification: NzNotificationService
  ) { 
    this.mill = this.route.snapshot.queryParams["mill"];// 从地址栏重获取参数 - 染厂
    this.clientId = this.route.snapshot.queryParams["clientId"];
    this.clientSid = this.route.snapshot.queryParams["clientSid"];
    this.clientName = this.route.snapshot.queryParams["clientName"];
    this.nickName = this.route.snapshot.queryParams["nickName"];
    this.openId = this.route.snapshot.queryParams["openId"];
    this.followTime = this.route.snapshot.queryParams["followTime"];
    this.code = this.route.snapshot.queryParams["code"];
    // 初始化时将真实姓名默认为昵称
    this.realName = this.nickName;

    if( !this.code ){
      console.log('进入页面初始化时候获取 code 失败，开始重新跳转到带有 code 的页面：' + this.url);
      //window.location.href = this.url;
    }else{
      console.log('页面初始化时候获取到的 code :' + this.code);
    }
  }

  ngOnInit() {
    this.code = this.route.snapshot.queryParams["code"];
    console.log('在 ngOnInit 中获取到的 code:' + this.code);
  }

  setEnterpriseCloud(){
    let info:FollowInfo = new FollowInfo();
    info.mill = this.mill;
    info.clientId=this.clientId;
    info.clientSid  = this.clientSid;
    info.clientName = this.clientName;
    info.openId = this.openId;
    info.nickName = this.nickName;
    info.realName = this.realName;
    this.service.setEnterpriseCloud(this.code,info).subscribe(
      (res:JsonResult) =>{
        if( res.code !=1 ){
          this.msg('设置企业云权限返回失败代码！');
        }
        
        this.msg('设置企业云权限成功！');
      },error => this.msg('设置企业云权限时出现异常！')
    )
  }

  setClientCloud(){
    let info:FollowInfo = new FollowInfo();
    info.mill = this.mill;
    info.clientId=this.clientId;
    info.clientSid  = this.clientSid;
    info.clientName = this.clientName;
    info.openId = this.openId;
    info.nickName = this.nickName;
    info.realName = this.realName;

    this.service.setClientCloud(this.code,info).subscribe(
      (res:JsonResult) =>{
        if( res.code !=1 ){
          this.msg('设置客户云权限返回失败代码！');
        }
        
        this.msg('设置客户云权限成功！');
      },error => this.msg('设置客户云权限时出现异常！')
    )
  }


  msg(content:string): void {
    this.notification.blank('通知',content);
  }



}
